/*
* AddTotalTime.java
* Created on  2014-8-13 下午3:07
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-8-13       fengxuanzhen    初始版本
*
*/
package com.genghis.ptas.util;

import com.genghis.ptas.course.entity.CourseRegister;

import java.util.List;

/**
 * 类的描述信息
 *
 * @author fengxuanzhen
 * @version 1.0.1
 */
public class AddTotalTime {

    public static String getTotalTime(List<CourseRegister> courseRegisterList) {
        String totalTime = "";
        String hour = "";
        String minuteForm = "";
        String secondForm = "";
        String msForm = "";
        int mss = 0;
        int ms = 0;
        int seconds = 0;
        int second = 0;
        int minutes = 0;
        int minute = 0;
        int hours = 0;
        int len = courseRegisterList.size();
        if (len == 0)
            return "0";
        if (len > 1) {
            for (int i = 1; i < len; i++) {
                String timeTemp = getTime(courseRegisterList.get(i - 1).getTotalTime(), courseRegisterList.get(i).getTotalTime());
                courseRegisterList.get(i - 1).setTotalTime(timeTemp);
            }
        }
        return courseRegisterList.get(len - 1).getTotalTime();
    }

    private static String getTime(String totalTime, String sessionTime) {
        if (!totalTime.equals("") && !sessionTime.equals("")) {
            int mss = Integer.parseInt(totalTime.substring(11)) + Integer.parseInt(sessionTime.substring(11));
            int ms = mss % 100;
            int seconds = Integer.parseInt(totalTime.substring(8, 10)) + Integer.parseInt(sessionTime.substring(8, 10)) + mss / 100;
            int second = (seconds) % 60;
            int minutes = Integer.parseInt(totalTime.substring(5, 7)) + Integer.parseInt(sessionTime.substring(5, 7)) + seconds / 60;
            int minute = (minutes) % 60;
            int hours = Integer.parseInt(totalTime.substring(0, 4)) + Integer.parseInt(sessionTime.substring(0, 4)) + minutes / 60;
            String hour = "0000" + hours;
            String minuteForm = "00" + minute;
            String secondForm = "00" + second;
            String msForm = "00" + ms;
            return hour.substring(hour.length() - 4, hour.length()) + ":" + minuteForm.substring(minuteForm.length() - 2, minuteForm.length()) + ":" + secondForm.substring(secondForm.length() - 2, secondForm.length()) + "." + msForm.substring(msForm.length() - 2, msForm.length());
        }
        if (totalTime.equals("") && !sessionTime.equals("")) {
            return sessionTime;
        }
        if (!totalTime.equals("") && sessionTime.equals("")) {
            return totalTime;
        }
        return "";
    }

}
